<?php
require_once("../../../config.inc.php");
require_once("../../../app/_inc/include.php");
require_once("./_inc/layoutFunction.php");

$aLabelColor = array("1"=>"#FF0000;", "2"=>"#FFFF00;", "3"=>"#FF00FF;");
$aFieldColor = array("1"=>"#FF0000;", "2"=>"#FFFF00;", "3"=>"#FF00FF;");

$errMsg = sxREQ("msg");
$layout = sxREQ("layout");
$band = sxREQ("band");

if ($band == '') {
	$condBand = "";
} else {
	$condBand = " AND lb.band='$band' ";
}

$sSQL = "SELECT lf.*, 
		     l.description AS lay_descr, lb.description AS band_descr,
		     l.max_colspan, l.type
		FROM layout l
		LEFT JOIN layout_band lb ON l.layout=lb.layout
		LEFT JOIN layout_field lf ON l.layout     = lf.layout " .
		                       " AND lb.band       = lf.band
		WHERE l.layout='$layout' $condBand
		ORDER BY lf.band, lf.row, lf.seq";
$objSDAO->query($sSQL, "lst");
$rsFieldList = $objSDAO->fetch("lst");
$max_colspan = $rsFieldList["max_colspan"];
$l_type = $rsFieldList["type"];
//output HTML
$aOpz = array("kMenu"=>'pro.lay');
echo(headerHTML($aOpz));

echo(bodyHTML($aOpz));

echo getLOHeader($layout);

$qsBand = ($band=='')?'':"&band=$band";
$aCmd = array("ins"=>array("pag"=>"layoutBandFieldIns.php?layout=$layout".$qsBand,
										"title"=>_t("new field"),
										"text"=>_t("new field"),
										"perm"=>"newmod"),
					"back"=>array()
					);
					
$sCmd = (commandsHTML($aCmd));	
echo $sCmd;

echo("<table class='tabl01' width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");
if ($l_type == 'form') {
	echo("<tr><td colspan='21' class='titl01'>Form layout</td></tr>");
	echo("<tr>");
	echo("<td class=\"tabh01\" style=\"text-align:center\" width='2%'>fascia/riga</td>\n");
	$endColspan = 20 - $max_colspan;
	$endWidth = 4.9*$endColspan;
	for($i=1;$i<=20;$i++) {
		if ($i > $max_colspan) {
			echo("<td class=\"tabh01\" style=\"text-align:center\" width='4.9%'>&nbsp;</td>\n");
		} else {
			echo("<td class=\"tabh01\" style=\"text-align:center\" width='4.9%'>$i</td>\n");
		}
	}
	$rsFieldList = $objSDAO->fetch("lst",0);
	while($rsFieldList) {
		if ($rsFieldList["row"]==''){
			break;
		}
		$numRow = $rsFieldList["row"];
		$band = $rsFieldList["band"];
		echo("<tr>");
		echo("<td class=\"text01\" valign='top'>".
			str_pad($band, 4, "0", STR_PAD_LEFT).'/'.str_pad($numRow, 4, "0", STR_PAD_LEFT)."</td>\n");
		$offSetCol = 0;
		while($rsFieldList and $numRow == $rsFieldList["row"] and $band == $rsFieldList["band"]) {
			$label_colspan = $rsFieldList["label_colspan"]; //($rsFieldList["label_colspan"])?$rsFieldList["label_colspan"]:1;
			$label_class = ($rsFieldList["label_style"]!='')?$rsFieldList["label_style"]:'labl01';
			$field_colspan = $rsFieldList["field_colspan"]; //($rsFieldList["field_colspan"])?$rsFieldList["field_colspan"]:1;
			$field_class = ($rsFieldList["field_style"]!='')?$rsFieldList["field_style"]:'text01';
			$labelStyle = "";
			if($rsFieldList["label_attr"] != ""){
				switch($rsFieldList["label_attr"]){
					case "normal":
						$labelStyle .= "font-style:normal;";
						break;
					case "bold":
						$labelStyle .= "font-weight:bold;";
						break;
					case "italic":
						$labelStyle .= "font-style:italic;";
						break;
					case "bold-italic":
						$labelStyle .= "font-weight:bold;font-style:italic;";
						break;
				}
			}
			if($rsFieldList["label_align"] != "" and $rsFieldList["label_align"] != "multi-line"){
				$labelStyle .= "text-align:".$rsFieldList["label_align"].";";
			}
			$fieldStyle = "";
			if($rsFieldList["field_attr"] != ""){
				switch($rsFieldList["field_attr"]){
					case "normal":
						$fieldStyle .= "font-style:normal;";
						break;
					case "bold":
						$fieldStyle .= "font-weight:bold;";
						break;
					case "italic":
						$fieldStyle .= "font-style:italic;";
						break;
					case "bold-italic":
						$fieldStyle .= "font-weight:bold;font-style:italic;";
						break;
				}
			}
			if($rsFieldList["field_align"] != "" and $rsFieldList["field_align"] != "multi-line"){
				$fieldStyle .= "text-align:".$rsFieldList["field_align"].";";
			}
			if ($offSetCol + $label_colspan > $max_colspan) { $label_colspan = $max_colspan - $offSetCol; }
			$offSetCol += $label_colspan;
			if($label_colspan != 0){
				$label = (trim($rsFieldList["label"])!='')?$rsFieldList["label"]:'-sp-';
				$pre1 = $pre2 = '';
				if ($rsFieldList["label_align"]=='multi-line') {
					$pre1 = "<pre class='$label_class'>$label";
					$pre2 = "</pre>";
					$label = " (".$rsFieldList["seq"].") ";
				}
				echo("<td colspan=\"$label_colspan\" class=\"$label_class\" style=\"$labelStyle\" valign='top'>$pre1");
				echo("<a class='$label_class' ".
							goToPag("layoutBandFieldMod.php?layout=".$rsFieldList["layout"]."&band=".$rsFieldList["band"]."&row=".$rsFieldList["row"]."&seq=".$rsFieldList["seq"]."&xsn=".XSN).
							" title=\""._t("Label '%s' - seq.: %s",$label,$rsFieldList["seq"])."\">".
							(($rsFieldList["label_align"]=='multi-line')?$label:substr($label,0,$label_colspan*6)).
							"</a>$pre2");
				echo("</td>");
			}
			if ($offSetCol + $field_colspan > $max_colspan) { $field_colspan = $max_colspan - $offSetCol; }
			$offSetCol += $field_colspan;
			if($field_colspan != 0){
				if ($rsFieldList["attribute"]!='') {
					$campo = $rsFieldList["layout_alias"]."_".$rsFieldList["attribute"];
				} elseif ($rsFieldList["variable"]!='') {
					$campo = "W_".$rsFieldList["variable"];
				} else {
					$campo = "-sp-";
				}
				echo("<td colspan=\"$field_colspan\" class=\"$field_class\" style=\"$fieldStyle\" valign='top'>");
				echo("<a class='$field_class' ".
							goToPag("layoutBandFieldMod.php?layout=".$rsFieldList["layout"]."&band=".$rsFieldList["band"]."&row=".$rsFieldList["row"]."&seq=".$rsFieldList["seq"]."&xsn=".XSN).
							" title=\""._t("Campo '%s' - seq.: %s",$campo,$rsFieldList["seq"])."\">".
							substr($campo,0,$field_colspan*6)."</a>");
				echo("</td>\n");
			}
			$rsFieldList = $objSDAO->fetch("lst");
		}
		for($j=$offSetCol;$j<$max_colspan;$j++){
			echo("<td class=\"text01\" valign='top'>&nbsp;</td>\n");
		}
		if ($endColspan > 0) {
			echo("<td class=\"text01\" style=\"text-align:center\" width='$endWidth%' colspan='$endColspan'>&nbsp;</td>\n");
		}	
		echo("</tr>");
	}
} else { // tipo layout set = table-line
	echo("<tr><td colspan='21' class='titl01'>List layout</td></tr>");
	echo("<tr>");
	$endColspan = 20 - $max_colspan;
	$endWidth = 5*$endColspan;
	for($i=1;$i<=20;$i++) {
		if ($i > $max_colspan) {
			echo("<td class=\"tabh01\" style=\"text-align:center\" width='5%'>&nbsp;</td>\n");
		} else {
			echo("<td class=\"tabh01\" style=\"text-align:center\" width='5%'>$i</td>\n");
		}
	}
	$rsFieldList = $objSDAO->fetch("lst",0);
	echo("<tr>");
	$offSetCol = 0;
	while($rsFieldList) {
		if ($rsFieldList["row"]==''){
			break;
		}
		$label_class = ($rsFieldList["label_style"]!='')?$rsFieldList["label_style"]:'labl01';
		$field_colspan = $rsFieldList["field_colspan"]; 
		$labelStyle = "";
		if($rsFieldList["label_attr"] != ""){
			switch($rsFieldList["label_attr"]){
				case "normal":
					$labelStyle .= "font-style:normal;";
					break;
				case "bold":
					$labelStyle .= "font-weight:bold;";
					break;
				case "italic":
					$labelStyle .= "font-style:italic;";
					break;
				case "bold-italic":
					$labelStyle .= "font-weight:bold;font-style:italic;";
					break;
			}
		}
		if($rsFieldList["label_align"] != "" and $rsFieldList["label_align"] != "multi-line"){
			$labelStyle .= "text-align:".$rsFieldList["label_align"].";";
		}
		if ($offSetCol + $field_colspan > $max_colspan) { $field_colspan = $max_colspan - $offSetCol; }
		$offSetCol += $field_colspan;
		if($field_colspan != 0){
			$label = (trim($rsFieldList["label"])!='')?$rsFieldList["label"]:'-sp-';
			$pre1 = $pre2 = '';
			if ($rsFieldList["label_align"]=='multi-line') {
				$pre1 = "<pre class='$label_class'>$label";
				$pre2 = "</pre>";
				$label = " (".$rsFieldList["seq"].") ";
			}
			echo("<td colspan=\"$field_colspan\" class=\"$label_class\" style=\"$labelStyle\" valign='top'>$pre1");
			echo("<a class='$label_class' ".
						goToPag("layoutBandFieldMod.php?layout=".$rsFieldList["layout"]."&band=".$rsFieldList["band"]."&row=".$rsFieldList["row"]."&seq=".$rsFieldList["seq"]."&xsn=".XSN).
						" title=\""._t("Etichetta '%s' - seq.: %s",$label,$rsFieldList["seq"])."\">".
						(($rsFieldList["label_align"]=='multi-line')?$label:substr($label,0,$field_colspan*6)).
						"</a>$pre2");
			echo("</td>");
		}
		$rsFieldList = $objSDAO->fetch("lst");
	}
	for($j=$offSetCol;$j<$max_colspan;$j++){
		echo("<td class=\"text01\" valign='top'>&nbsp;</td>\n");
	}
	if ($endColspan > 0) {
		echo("<td class=\"text01\" style=\"text-align:center\" width='$endWidth%' colspan='$endColspan'>&nbsp;</td>\n");
	}	
	echo("</tr>");
	$rsFieldList = $objSDAO->fetch("lst",0);
	echo("<tr>");
	$offSetCol = 0;
	while($rsFieldList) {
		if ($rsFieldList["row"]==''){
			break;
		}
		$field_colspan = $rsFieldList["field_colspan"]; //($rsFieldList["field_colspan"])?$rsFieldList["field_colspan"]:1;
		$field_class = ($rsFieldList["field_style"]!='')?$rsFieldList["field_style"]:'text01';
		$fieldStyle = "";
		if($rsFieldList["field_attr"] != ""){
			switch($rsFieldList["field_attr"]){
				case "normal":
					$fieldStyle .= "font-style:normal;";
					break;
				case "bold":
					$fieldStyle .= "font-weight:bold;";
					break;
				case "italic":
					$fieldStyle .= "font-style:italic;";
					break;
				case "bold-italic":
					$fieldStyle .= "font-weight:bold;font-style:italic;";
					break;
			}
		}
		if($rsFieldList["field_align"] != "" and $rsFieldList["field_align"] != "multi-line"){
			$fieldStyle .= "text-align:".$rsFieldList["field_align"].";";
		}
		if ($offSetCol + $field_colspan > $max_colspan) { $field_colspan = $max_colspan - $offSetCol; }
		$offSetCol += $field_colspan;
		if($field_colspan != 0){
			if ($rsFieldList["attribute"]!='') {
				$campo = $rsFieldList["layout_alias"]."_".$rsFieldList["attribute"];
			} elseif ($rsFieldList["variable"]!='') {
				$campo = "W_".$rsFieldList["variable"];
			} else {
				$campo = "-sp-";
			}
			echo("<td colspan=\"$field_colspan\" class=\"$field_class\" style=\"$fieldStyle\" valign='top'>");
			echo("<a class='$field_class' ".
						goToPag("layoutBandFieldMod.php?layout=".$rsFieldList["layout"]."&band=".$rsFieldList["band"]."&row=".$rsFieldList["row"]."&seq=".$rsFieldList["seq"]."&xsn=".XSN).
						" title=\""._t("Campo '%s' - seq.: %s",$campo,$rsFieldList["seq"])."\">".
						substr($campo,0,$field_colspan*6)."</a>");
			echo("</td>\n");
		}
		$rsFieldList = $objSDAO->fetch("lst");
	}
	for($j=$offSetCol;$j<$max_colspan;$j++){
		echo("<td class=\"text01\" valign='top'>&nbsp;</td>\n");
	}
	if ($endColspan > 0) {
		echo("<td class=\"text01\" style=\"text-align:center\" width='$endWidth%' colspan='$endColspan'>&nbsp;</td>\n");
	}	
	echo("</tr>");
}
echo("</table>\n");

echo $sCmd;
echo(footerHTML());
exit();